package com.wzg.mytableview.smart;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.bin.david.form.core.SmartTable;
import com.bin.david.form.data.column.Column;
import com.bin.david.form.data.format.draw.ImageResDrawFormat;
import com.bin.david.form.data.format.draw.TextImageDrawFormat;
import com.bin.david.form.data.table.TableData;
import com.bin.david.form.listener.OnColumnItemClickListener;
import com.bin.david.form.utils.DensityUtils;
import com.wzg.mytableview.R;

import java.util.ArrayList;
import java.util.LinkedList;

/**
 * author：wzg
 * date；
 * desc:这是我们公司常用的erp app常用格式
 */
public class CommonActivity extends AppCompatActivity {


    private SmartTable<Material3> smartTable;


    private LinkedList<Material3> material3s = new LinkedList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_common);

        smartTable = findViewById(R.id.smartTable);


        for (int i = 0; i < 30; i++) {
            if (i == 22) {
                material3s.add(new Material3(true, "" + i, "22222222222222222222222", "33"));
            } else material3s.add(new Material3(true, "" + i, "22", "33"));

        }

        int size = DensityUtils.dp2px(this, 15);
        int size1 = DensityUtils.dp2px(this, 8);
//        Column<String> deleteColumn = new Column<>("delete", "delete", new TextImageDrawFormat<String>(size, size, 2, size1) {
//            @Override
//            protected Context getContext() {
//                return CommonActivity.this;
//            }
//
//            @Override
//            protected int getResourceID(String s, String value, int position) {
////                setDirection(TextImageDrawFormat.LEFT);
//                return R.mipmap.name;
//            }
//        });
        Column<Boolean> deleteColumn = new Column<Boolean>("操作", "delete", new ImageResDrawFormat<Boolean>(size, size) {
            @Override
            protected Context getContext() {
                return CommonActivity.this;
            }

            @Override
            protected int getResourceID(Boolean aBoolean, String value, int position) {
                return R.mipmap.name;
            }
        });
//        deleteColumn.setOnColumnItemClickListener(new OnColumnItemClickListener<String>() {
//            @Override
//            public void onClick(Column<String> column, String value, String s, int position) {
////                column.getDatas().set(position,value);
////                smartTable.invalidate();
//            }
//        });

        Column<String> no = new Column<>("物料名称", "materialNo");
        Column<String> num = new Column<>("入库数", "num");
        Column<String> realNum = new Column<>("实际入库数", "realNum");

        TableData<Material3> tableData = new TableData<Material3>("", material3s, deleteColumn, no, realNum, num);


        smartTable.getConfig().setShowXSequence(false);
        smartTable.getConfig().setShowYSequence(false);
        smartTable.getConfig().setShowColumnTitle(true);
        smartTable.getConfig().setShowTableTitle(false);
        smartTable.setTableData(tableData);
        tableData.setOnItemClickListener(new TableData.OnItemClickListener() {
            @Override
            public void onClick(Column column, String value, Object o, int col, int row) {

                try {
                    if (column == deleteColumn) {
                        /**
                         * 这里一定要加延时，否则界面还在绘画时，被刷新就会奔溃
                         * desc：因为看到作者的源码时可以发现时使用线程也就是存在未知性
                         */
                        smartTable.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(CommonActivity.this, "删除  col: " + col + "\trow: " + row + "个", Toast.LENGTH_LONG).show();
                                material3s.remove(row);
                                TableData<Material3> tableData = new TableData<>("", material3s, deleteColumn, no, realNum, num);
                                /**
                                 * 一定要调用这个，否则会出现闪屏问题
                                 */
                                smartTable.invalidate();
                                smartTable.setTableData(tableData);
                            }
                        }, 1000);

//                        smartTable.notifyDataChanged();
//                        smartTable.invalidate();

                    }
                } catch (Throwable e) {
                    Log.e("Throwable", e.getMessage());
                }


            }
        });


    }
}